﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
using System.Text;
using System.IO;
public partial class admin_Admin_NewsList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            this.GetNewsPages();
        }

    }

    private void GetNewsPages()
    {
       
        News news = new News();
        AspNetPager1.RecordCount = news.GetNewsCount();
        DataSet dr = news.GetNewsPage(AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1), AspNetPager1.PageSize, "newspage");
        DataTable da = dr.Tables["newspage"];
        NewsList.DataSource = da;
        NewsList.DataKeyNames = new string[] { "NID" };//主键
        NewsList.DataBind();
    }

    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        GetNewsPages();
    }
    protected void NewsList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        //限制标题换行
        e.Row.Cells[1].Attributes.Add("style", "word-break :break-all ; word-wrap:break-word"); 
        //鼠标换背景色
        int i;
        //执行循环，保证每条数据都可以更新
        for (i = 0; i < NewsList.Rows.Count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#E6F5FA'");
                //当鼠标移开时还原背景色
                e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }
         //删除提醒框
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
             if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                ((LinkButton)e.Row.Cells[4].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除：\"" + e.Row.Cells[1].Text + "\"吗?')");
            }
        } 

    }


    //删除
    protected void NewsList_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        News delnews = new News();
        delnews.DelNews(Convert.ToInt32(NewsList.DataKeys[e.RowIndex].Value.ToString()));
        GetNewsPages();
    }


    protected void Button1_Click(object sender, EventArgs e)
    {
        
        News delallnews = new News();
          for (int i = 0; i < NewsList.Rows.Count; i++) 
           {
               CheckBox cbox = (CheckBox)NewsList.Rows[i].Cells[0].FindControl("del");
               if (cbox.Checked == true) 
               {
                   delallnews.DelNews(Convert.ToInt32(NewsList.DataKeys[i].Value));
              }
           }
      GetNewsPages();
      Button2.Text = "全选";

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        CheckBox cb;
        if (Button2.Text == "全选")
        {
            for (int i = 0; i < NewsList.Rows.Count; i++)
            {
                cb = (CheckBox)NewsList.Rows[i].Cells[0].FindControl("del");
                cb.Checked = true;
            }
            Button2.Text = "取消";
        }
        else
        {
            for (int i = 0; i < NewsList.Rows.Count; i++)
            {
                cb = (CheckBox)NewsList.Rows[i].Cells[0].FindControl("del");
                cb.Checked = false;
            }
            Button2.Text = "全选";
        }

    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        GetTags GetT = new GetTags();
        string Toplianjie = WebSet.GetSingleValue("顶部连接");
        string hongxingwangzhan = WebSet.GetSingleValue("红星网站导航");
        string ProList = WebSet.GetTopnProNoPic(14);
        string NewsList = WebSet.GetTopnNews(10, 18);
        string DibuLianjie = WebSet.GetSingleValue("底部版权");
        string yuming = WebSet.GetSingleValue("网站域名");
        string WebTitle = WebSet.GetSingleValue("WebTitle").ToString();

        News ne = new News();
        OleDbDataReader dr = ne.GetNewsAllDR();
        while (dr.Read())
        {
            string danqiandizhi =  dr["Htmlurl"].ToString();
            string Title1 = dr["Ntitle"].ToString();
            string Title2 = dr["Ntitle2"].ToString();
            string Key = dr["Nkey"].ToString();
            string des = dr["description"].ToString();
            string conn = dr["Ncon"].ToString() + "<br/>本文地址：<a href=\"" + yuming + "/" + danqiandizhi + "\">" + yuming + "/" + danqiandizhi + "</a>";

            if (CheckBox1.Checked)
            {
                conn = SetConnLink.GetNoneLink(conn);
                conn = SetConnLink.FormatBody(conn);
            }
            if (CheckBox2.Checked)
            {
                conn = conn + GetT.GetXiangGuanWenZhang(Key, dr["Htmlurl"].ToString());
            }
            Title2 += WebTitle;
            //生成页面
            try
            {
                StringBuilder strhtml = new StringBuilder();
                //创建StreamReader对象
                using (StreamReader sr = new StreamReader(System.Web.HttpContext.Current.Server.MapPath("~/") + "newsMoBan.html", Encoding.GetEncoding("gb2312")))
                {
                    String oneline;
                    //读取指定的HTML文件模板
                    while ((oneline = sr.ReadLine()) != null)
                    {
                        strhtml.Append(oneline);
                    }
                    sr.Close();
                }
                string FilePath = "";
                strhtml = strhtml.Replace("$Title2", Title2);
                strhtml = strhtml.Replace("$keywords", Key);
                strhtml = strhtml.Replace("$description", des);
                strhtml = strhtml.Replace("$Ntitle1", Title1);
                strhtml = strhtml.Replace("$conn", conn);
                strhtml = strhtml.Replace("$dangqiandizhi", danqiandizhi);
                strhtml = strhtml.Replace("$Toplianjie", Toplianjie);
                //strhtml = strhtml.Replace("$dangqiandizhi", hongxingwangzhan);
                strhtml = strhtml.Replace("$ProList", ProList);
                strhtml = strhtml.Replace("$DibuLianjie", DibuLianjie);
                strhtml = strhtml.Replace("$NewsList", NewsList);

                FilePath = System.Web.HttpContext.Current.Server.MapPath("~/") + danqiandizhi;
                //删除原有文件
                FileInfo file = new FileInfo(FilePath);
                if (file.Exists)
                {
                    file.Delete();
                }
                //创建文件信息对象--------------------------------------------
                FileInfo finfo = new FileInfo(FilePath);
                //以打开或者写入的形式创建文件流
                using (FileStream fs = finfo.OpenWrite())
                {
                    //根据上面创建的文件流创建写数据流
                    StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.Default);
                    //把新的内容写到创建的HTML页面中
                    sw.WriteLine(strhtml);
                    sw.Flush();
                    sw.Close();
                }

            }
            catch (Exception err)
            {
                //输出异常信息
                throw new Exception(err.Message, err);
            }

        }
        dr.Close();
        dr.Dispose();
        Response.Write("<script>alert('生成完成！')</script>");
    }
}
